<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix flex_wrap">
        <el-form inline size="small" :model="tableFrom" ref="tableFrom" label-width="100px">
          <el-col :xs="24" :sm="24" :md="24" :lg="20" :xl="20">
            <el-form-item label="数据场：">
              <UserSearchable v-model="tableFrom.dataFactoryId" :type="'datafieldList'" class="w_200">
                <template #dropdownTitle>
                  <span>数据场名称</span>
                  <span>负责人</span>
                </template>
                <template v-slot:dropdown="{ item }">
                  <span>{{ item.name }}</span>
                  <span>{{ item.principal }}</span>
                </template>
              </UserSearchable>
            </el-form-item>
            <el-form-item label="用户：">
              <UserSearchable v-model="tableFrom.userId" class="w_200"> </UserSearchable>
            </el-form-item>
            <el-form-item label="分组">
              <el-select v-model="tableFrom.groupId" placeholder="请选择分组" clearable class="w_120">
                <el-option :value="item.id" v-for="(item, index) in groupList" :key="index"
                  :label="item.groupName"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="标签">
              <el-select multiple v-model="searchFrom.tagId" placeholder="请选择标签" clearable class="w_250">
                <el-option :value="item.id" v-for="(item, index) in labelLists" :key="index"
                  :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="开始时间：">
              <el-date-picker v-model="tableFrom.startTime" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                placeholder="选择日期" clearable class="selWidth">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间：">
              <el-date-picker v-model="tableFrom.endTime" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                placeholder="选择日期" clearable class="selWidth">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="4" :xl="4">
            <el-form-item>
              <el-button class="mr10" size="small" type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
              <el-button class="mr10" size="small" icon="el-icon-refresh" @click="reset()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </div>
      <el-table border v-loading="listLoading" :data="tableData.data" style="width: 100%" size="mini" class="table"
        highlight-current-row>
        <el-table-column prop="userId" label="UID" min-width="50" />
        <el-table-column label="头像" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image :src="scope.row.avatar" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="nickName" label="用户昵称" min-width="150" />
        <el-table-column prop="dataFactoryName" label="数据场" min-width="100" />
        <el-table-column prop="amount" label="交易金额" min-width="100">
          <template slot-scope="scope">
            <span>{{
              parseFloat(`${scope.row.amount}`)
                .toFixed(2)
                .replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="approvalStatus" label="状态" min-width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.approvalStatus == 1">待审核</span>
            <span v-if="scope.row.approvalStatus == 2">已通过</span>
            <span v-if="scope.row.approvalStatus == 3">已拒绝</span>
          </template>
        </el-table-column> -->
        <el-table-column prop="operatorName" label="操作员" min-width="100" />
        <el-table-column prop="operatorType" label="前后台类型" min-width="100">
          <template slot-scope="scope">
            {{
              scope.row.operatorType == 1
                ? "其他"
                : scope.row.operatorType == 2
                  ? "后台"
                  : "手机用户"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="100" />
        <el-table-column prop="createTime" label="创建时间" min-width="200" />
        <!-- <el-table-column prop="approvalDate" label="审核时间" min-width="200" /> -->
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
          @current-change="pageChange" />
      </div>
    </el-card>

    <!--用户列表-->
    <el-dialog title="用户列表" :visible.sync="userVisible" width="900px" :close-on-click-modal="false"
      :close-on-press-escape="false">
      <user-list v-if="userVisible" @getTemplateRow="getTemplateRow"></user-list>
      <span slot="footer" class="dialog-footer">
        <el-button @click="userclose()">取 消</el-button>
        <el-button type="primary" @click="usersubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { walletbillCapitalFlow } from "@/api/financial";
import userList from "@/components/userList";

import UserSearchable from "@/components/UserSearchable";
import {
  tagListApi,
  groupListApi
} from '@/api/user'

export default {
  components: {
    userList,
    UserSearchable,
  },
  data() {
    return {
      groupList: [],
      labelLists: [],
      tableFrom: {
        groupId: "",
        tagId: "",
        dataFactoryId: "",
        userId: "",
        startTime: "",
        endTime: "",
        page: 1,
        limit: 20,
      },
      searchFrom: {
        tagId: [],
      },

      userVisible: false,
      cardLists: [],
      listLoading: false,
      tableData: {
        data: [],
        total: 0,
      },
    };
  },
  mounted() {
    this.getlist();
    // 分组、标签
    this.groupLists();
    this.getTagList();
  },
  methods: {
    // 分组列表
    groupLists() {
      groupListApi({ page: 1, limit: 9999 }).then(async (res) => {
        this.groupList = res.list;
      });
    },
    //标签列表
    getTagList() {
      tagListApi({ page: 1, limit: 9999 }).then((res) => {
        this.labelLists = res.list;
      });
    },
    selectUser() {
      this.userVisible = true;
    },
    getTemplateRow(row) {
      this.tableFrom.userId = row.uid;
    },
    userclose() {
      this.tableFrom.userId = "";
      this.userVisible = false;
    },
    usersubmit() {
      this.userVisible = false;
    },
    search() {
      this.tableFrom.page = 1;
      this.tableFrom.limit = 20;
      if (this.searchFrom.tagId.length > 0) {
        let tagId = this.searchFrom.tagId.join(",")
        this.tableFrom.tagId = tagId
      }

      this.getlist();
    },
    reset() {
      this.tableFrom = {
        tagId: "",
        groupId: "",
        dataFactoryId: "",
        userId: "",
        startTime: "",
        endTime: "",
        page: 1,
        limit: 20,
      };
      this.searchFrom.tagId = []
      this.getlist();
    },
    getlist() {
      this.listLoading = true;
      walletbillCapitalFlow(this.tableFrom)
        .then((res) => {
          console.log("资金监控", res);
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getlist();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getlist();
    },
  },
};
</script>

<style lang="scss" scoped>
.flex_wrap {
  display: flex;
  flex-wrap: wrap;
}
.w-60 {
  width: 60%;
}

.w_200 {
  width: 220px;
}

.w_120 {
  width: 120px;
}

.w_250 {
  width: 250px;
}
</style>
